// pages/indexShare/indexShare.js
var common = require("../../../common/common.js").common;

var leader = require("../../../common/leader.js").leader;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        s_leader: [],
        myleaderinfo: {
            leader_id: 1,
            leader_id_true: -1
        }
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        let that = this;
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        if (options.myleaderinfo) {
            that.setData({
                myleaderinfo: JSON.parse(decodeURIComponent(options.myleaderinfo))
            });
        }
        if (options.uid) {
            that.setData({
                uid: options.uid
            });
        }
        wx.getSystemInfo({
            success: function(res) {
                let rpx = res.windowWidth / 375;
                that.setData({
                    rpx: rpx
                });
            }
        });
        // that.getBase();
        },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {
        let that = this;
        that.getBase();
    },
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    switchIndex: function(e) {
        //返回首页
        wx.switchTab({
            url: "/pages/index/index"
        });
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                let wxapp_img = res.extra.wxapp_img ? res.extra.wxapp_img : "";
                if (wxapp_img.indexOf("https:") < 0) {
                    wxapp_img = wxapp_img.replace("http:", "https:");
                }
                that.setData({
                    wxapp_img: wxapp_img,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    title: res.extra.title ? res.extra.title : "社区团购"
                });
                that.makeposter();
            }
        });
    },
    //生成海报
    makeposter: function() {
        var that = this;
        var my_leaderID = that.data.myleaderinfo.leader_id;
        const ctx = wx.createCanvasContext("firstCanvas");
        common.request({
            endpoint: "share/poster_data2",
            method: "post",
            data: {
                leader_id: my_leaderID,
                uid: that.data.uid
            },
            success: res => {
                let poster_background = res.extra.poster_background;
                if (poster_background.indexOf("https:") < 0) {
                    poster_background = poster_background.replace("http:", "https:");
                }
                let qc = res.extra.qc;
                if (qc.indexOf("https:") < 0) {
                    qc = qc.replace("http:", "https:");
                }
                that.setData({
                    poster_info: res.extra
                });
                let poster_mode = res.extra.poster_mode;
                let poster_data = res.extra.poster_data.pro;
                let poster_color = res.extra.poster_color == 1 ? "#fff" : "#333";
                if (!poster_data.length) {
                    wx.showToast({
                        title: "暂无推荐商品",
                        icon: "none"
                    });
                    setTimeout(() => {
                        wx.navigateBack({});
                    }, 2e3);
                    return;
                }
                wx.showLoading({
                    title: "海报生成中"
                });
                wx.getImageInfo({
                    src: poster_background,
                    success: function(res) {
                        ctx.drawImage(res.path, 0, 0, 375 * that.data.rpx, 667 * that.data.rpx);
                        if (poster_background.indexOf("bg") != -1) {
                            var bg_id = poster_background.substr(poster_background.indexOf("bg") + 2, 1);
                            switch (bg_id) {
                              case "1":
                                ctx.drawImage(that.data.domain + "/img/mainPack/type1_title.png", 105 * that.data.rpx, 52 * that.data.rpx, 163.5 * that.data.rpx, 18 * that.data.rpx);
                                break;

                              case "2":
                                ctx.drawImage(that.data.domain + "/img/mainPack/type2_title.png", 150 * that.data.rpx, 52 * that.data.rpx, 83 * that.data.rpx, 32 * that.data.rpx);
                                break;

                              case "3":
                                ctx.drawImage(that.data.domain + "/img/mainPack/type5_title.png", 80 * that.data.rpx, 45 * that.data.rpx, 190.5 * that.data.rpx, 17 * that.data.rpx);
                                break;

                              case "4":
                                that.makeWxapp_img(ctx, {
                                    callback() {
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 17px sans-serif";
                                        ctx.fillText(that.data.title, 160 * that.data.rpx, 55 * that.data.rpx);
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 9px sans-serif";
                                        ctx.fillText("实惠团购  好物尽享", 160 * that.data.rpx, 72 * that.data.rpx);
                                        ctx.draw(true);
                                    }
                                });
                                break;

                              case "5" || "6":
                                that.makeWxapp_img(ctx, {
                                    callback() {
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 17px sans-serif";
                                        ctx.fillText(that.data.title, 160 * that.data.rpx, 55 * that.data.rpx);
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 9px sans-serif";
                                        ctx.fillText("实惠团购  好物尽享", 160 * that.data.rpx, 72 * that.data.rpx);
                                        ctx.draw(true);
                                    }
                                });
                                break;
                            }
                        }
                        wx.getImageInfo({
                            src: qc,
                            success: function(res) {
                                that.makePosterInfo(ctx, {
                                    callback() {
                                        ctx.arc(305 * that.data.rpx, 605 * that.data.rpx, 45 * that.data.rpx, 0, 2 * Math.PI);
                                        ctx.setFillStyle("#fff");
                                        ctx.fill();
                                        ctx.drawImage(res.path, 260 * that.data.rpx, 562 * that.data.rpx, 90 * that.data.rpx, 90 * that.data.rpx);
                                        if (poster_mode == 2) {
                                            //三列商品
                                            that.makeItem_type2(ctx, poster_data, 0);
                                        } else if (poster_mode == 1) {
                                            that.makeItem_type1(ctx, poster_data, 0);
                                        }
                                    }
                                });
                            },
                            fail(res) {
                                wx.hideLoading();
                                wx.showModal({
                                    title: "提示",
                                    content: "图片获取失败,图片地址为" + qc,
                                    showCancel: false
                                });
                            }
                        });
                    },
                    fail(res) {
                        wx.hideLoading();
                        wx.showModal({
                            title: "提示",
                            content: "图片获取失败,图片地址为" + poster_background,
                            showCancel: false
                        });
                    }
                });
            },
            fail() {
                wx.hideLoading();
                wx.navigateBack({});
            }
        });
    },
    makeWxapp_img: function(ctx, options) {
        let that = this;
        if (that.data.wxapp_img) {
            wx.getImageInfo({
                src: that.data.wxapp_img,
                success: function(res) {
                    ctx.drawImage(res.path, 120 * that.data.rpx, 40 * that.data.rpx, 32.5 * that.data.rpx, 32.5 * that.data.rpx);
                    options.callback();
                },
                fail(res) {
                    wx.hideLoading();
                    wx.showModal({
                        title: "提示",
                        content: "图片获取失败,图片地址为" + that.data.wxapp_img,
                        showCancel: false
                    });
                }
            });
        } else {
            options.callback();
        }
    },
    makePosterInfo: function(ctx, options) {
        let that = this;
        let leaderinfo_other = that.data.myleaderinfo.other ? that.data.myleaderinfo.other : "-";
        //社区
                let leaderinfo_name = that.data.myleaderinfo.name ? that.data.myleaderinfo.name : "-";
        let leaderinfo_title = that.data.myleaderinfo.title ? that.data.myleaderinfo.title : "-";
        //详细地址
                let leaderinfo_img = that.data.myleaderinfo.img ? that.data.myleaderinfo.img : "/images/default.png";
        let is_level = that.data.poster_info.is_level;
        let poster_set = that.data.poster_info.poster_set;
        let level_name = that.data.poster_info.level_name;
        let level_nickname = that.data.poster_info.level_nickname;
        let level_img = that.data.poster_info.level_img;
        let level_phone = that.data.poster_info.level_phone;
        let poster_color = that.data.poster_info.poster_color == 1 ? "#fff" : "#333";
        if (is_level == 1 && poster_set == 2) {
            //仅分销商
            wx.getImageInfo({
                src: level_img,
                success(res) {
                    ctx.save();
                    ctx.beginPath();
                    ctx.arc(40.5 * that.data.rpx, 582.5 * that.data.rpx, 12.5 * that.data.rpx, 0, 2 * Math.PI);
                    ctx.clip();
                    ctx.drawImage(res.path, 28 * that.data.rpx, 570 * that.data.rpx, 25 * that.data.rpx, 25 * that.data.rpx);
                    ctx.restore();
                    ctx.draw(true);
                    ctx.setFillStyle(poster_color);
                    ctx.font = "normal bold 14px sans-serif";
                    ctx.fillText(level_nickname, 63 * that.data.rpx, 590 * that.data.rpx);
                    // ctx.font =  'normal bold 13px sans-serif';
                    // ctx.fillText("姓名："+level_name, 27.5 * that.data.rpx,614* that.data.rpx);
                    // ctx.fillText("电话："+level_phone, 27.5  * that.data.rpx, 637* that.data.rpx);
                                        options.callback();
                }
            });
        } else if (is_level == 1 && poster_set == 3) {
            //2者都有
            wx.getImageInfo({
                src: level_img,
                success(res) {
                    ctx.save();
                    ctx.beginPath();
                    ctx.arc(43 * that.data.rpx, 588 * that.data.rpx, 15 * that.data.rpx, 0, 2 * Math.PI);
                    ctx.clip();
                    ctx.drawImage(res.path, 28 * that.data.rpx, 573 * that.data.rpx, 30 * that.data.rpx, 30 * that.data.rpx);
                    ctx.restore();
                    ctx.draw(true);
                    ctx.setFillStyle("#fff");
                    ctx.font = "normal bold 12px sans-serif";
                    ctx.fillText(level_nickname, 68.5 * that.data.rpx, 585 * that.data.rpx);
                    // ctx.setFillStyle("#fff");
                    // ctx.font =  'normal bold 11px sans-serif';
                    // ctx.fillText(level_name+' '+level_phone, 68.5 * that.data.rpx, 604* that.data.rpx);
                                        that.dealWords({
                        ctx: ctx,
                        font: "normal bold 11px sans-serif",
                        FillStyle: "#fff",
                        word: leaderinfo_name + "-" + leaderinfo_title,
                        maxWidth: 200 * that.data.rpx,
                        x: 40.5 * that.data.rpx,
                        y: 604 * that.data.rpx,
                        maxLine: 1
                    });
                    that.dealWords({
                        ctx: ctx,
                        font: "11px sans-serif",
                        FillStyle: "#fff",
                        word: leaderinfo_other,
                        maxWidth: 200 * that.data.rpx,
                        x: 40.5 * that.data.rpx,
                        y: 624 * that.data.rpx,
                        maxLine: 1
                    });
                    options.callback();
                }
            });
        } else {
            that.dealWords({
                //团长名称
                ctx: ctx,
                fontSize: 14,
                FillStyle: poster_color,
                word: that.data.leaderName + "：" + (that.data.myleaderinfo.name ? that.data.myleaderinfo.name : "-"),
                maxWidth: 200 * that.data.rpx,
                x: 22 * that.data.rpx,
                y: 580 * that.data.rpx,
                maxLine: 1
            });
            that.dealWords({
                //团长名称
                ctx: ctx,
                fontSize: 14,
                FillStyle: poster_color,
                word: "提货地址：" + (that.data.myleaderinfo.other ? that.data.myleaderinfo.other : "-"),
                maxWidth: 200 * that.data.rpx,
                x: 22 * that.data.rpx,
                y: 600 * that.data.rpx,
                maxLine: 2
            });
        }
        options.callback();
    },
    makeItem_type2(ctx, poster_data, param) {
        //三列
        let that = this;
        if (param == poster_data.length || param == 9) {
            wx.hideLoading();
            console.log("绘制完成");
            ctx.draw(true, () => {
                that.canvasToImage();
            });
            return;
        }
        const name = poster_data[param].name;
        const min_price = "￥" + poster_data[param].min_price;
        let icon = poster_data[param].icon;
        if (icon.indexOf("https:") < 0) {
            icon = icon.replace("http:", "https:");
        }
        wx.getImageInfo({
            src: icon,
            success: function(res) {
                console.log("param++++" + param);
                if (param < 3) {
                    let k = param;
                    ctx.drawImage(res.path, (22 + 115 * k) * that.data.rpx, 120 * that.data.rpx, 100 * that.data.rpx, 100 * that.data.rpx);
                    that.dealWords({
                        ctx: ctx,
                        //画布上下文
                        fontSize: 14,
                        //字体大小
                        FillStyle: "#333",
                        //字体颜色
                        word: name,
                        //需要处理的文字
                        maxWidth: 100 * that.data.rpx,
                        //一行文字最大宽度
                        x: (22 + 115 * k) * that.data.rpx,
                        //文字在x轴要显示的位置
                        y: 220 * that.data.rpx,
                        //文字在y轴要显示的位置
                        maxLine: 1
                    });
                    that.dealWords({
                        ctx: ctx,
                        fontSize: 14,
                        FillStyle: "#e51a00",
                        word: min_price,
                        maxWidth: 100 * that.data.rpx,
                        x: (22 + 115 * k) * that.data.rpx,
                        y: 240 * that.data.rpx,
                        maxLine: 1
                    });
                    if (param < poster_data.length) {
                        that.makeItem_type2(ctx, poster_data, param + 1);
                    }
                } else if (param >= 3 && param < 6) {
                    let k = param - 3;
                    ctx.drawImage(res.path, (22 + 115 * k) * that.data.rpx, 265 * that.data.rpx, 100 * that.data.rpx, 100 * that.data.rpx);
                    that.dealWords({
                        ctx: ctx,
                        //画布上下文
                        fontSize: 14,
                        //字体大小
                        FillStyle: "#333",
                        //字体颜色
                        word: name,
                        //需要处理的文字
                        maxWidth: 100 * that.data.rpx,
                        //一行文字最大宽度
                        x: (22 + 115 * k) * that.data.rpx,
                        //文字在x轴要显示的位置
                        y: 365 * that.data.rpx,
                        //文字在y轴要显示的位置
                        maxLine: 1
                    });
                    that.dealWords({
                        ctx: ctx,
                        fontSize: 14,
                        FillStyle: "#e51a00",
                        word: min_price,
                        maxWidth: 100 * that.data.rpx,
                        x: (22 + 115 * k) * that.data.rpx,
                        y: 385 * that.data.rpx,
                        maxLine: 1
                    });
                    if (param < poster_data.length) {
                        that.makeItem_type2(ctx, poster_data, param + 1);
                    }
                } else if (param >= 6 && param < 9) {
                    let k = param - 6;
                    ctx.drawImage(res.path, (22 + 115 * k) * that.data.rpx, 410 * that.data.rpx, 100 * that.data.rpx, 100 * that.data.rpx);
                    that.dealWords({
                        ctx: ctx,
                        //画布上下文
                        fontSize: 14,
                        //字体大小
                        FillStyle: "#333",
                        //字体颜色
                        word: name,
                        //需要处理的文字
                        maxWidth: 100 * that.data.rpx,
                        //一行文字最大宽度
                        x: (22 + 115 * k) * that.data.rpx,
                        //文字在x轴要显示的位置
                        y: 510 * that.data.rpx,
                        //文字在y轴要显示的位置
                        maxLine: 1
                    });
                    that.dealWords({
                        ctx: ctx,
                        fontSize: 14,
                        FillStyle: "#e51a00",
                        word: min_price,
                        maxWidth: 100 * that.data.rpx,
                        x: (22 + 115 * k) * that.data.rpx,
                        y: 527 * that.data.rpx,
                        maxLine: 1
                    });
                    if (param < poster_data.length) {
                        that.makeItem_type2(ctx, poster_data, param + 1);
                    }
                }
            },
            fail(res) {
                wx.hideLoading();
                wx.showModal({
                    title: "提示",
                    content: "图片获取失败,图片地址为" + icon,
                    showCancel: false
                });
                if (param < poster_data.length) {
                    that.makeItem_type2(ctx, poster_data, param + 1);
                }
            }
        });
    },
    makeItem_type1: function(ctx, poster_data, param) {
        let that = this;
        if (param == poster_data.length || param == 10) {
            wx.hideLoading();
            console.log("绘制完成");
            ctx.draw(true, () => {
                that.canvasToImage();
            });
            return;
        }
        let name = poster_data[param].name;
        let min_price = "￥" + poster_data[param].min_price;
        let icon = poster_data[param].icon;
        if (icon.indexOf("https:") < 0) {
            icon = icon.replace("http:", "https:");
        }
        wx.getImageInfo({
            src: icon,
            success: function(res) {
                if (param < 2) {
                    let k = param;
                    ctx.drawImage(res.path, (22 + 180 * k) * that.data.rpx, 120 * that.data.rpx, 70 * that.data.rpx, 70 * that.data.rpx);
                    that.dealWords({
                        ctx: ctx,
                        //画布上下文
                        fontSize: 14,
                        //字体大小
                        FillStyle: "#333",
                        //字体颜色
                        word: name,
                        //需要处理的文字
                        maxWidth: 80 * that.data.rpx,
                        //一行文字最大宽度
                        x: (102 + 180 * k) * that.data.rpx,
                        //文字在x轴要显示的位置
                        y: 120 * that.data.rpx,
                        //文字在y轴要显示的位置
                        maxLine: 2
                    });
                    that.dealWords({
                        ctx: ctx,
                        fontSize: 14,
                        FillStyle: "#e51a00",
                        word: min_price,
                        maxWidth: 80 * that.data.rpx,
                        x: (102 + 180 * k) * that.data.rpx,
                        y: 160 * that.data.rpx,
                        maxLine: 1
                    });
                    if (param < poster_data.length) {
                        that.makeItem_type1(ctx, poster_data, param + 1);
                    }
                } else if (param >= 2 && param < 4) {
                    let k = param - 2;
                    ctx.drawImage(res.path, (22 + 180 * k) * that.data.rpx, 210 * that.data.rpx, 70 * that.data.rpx, 70 * that.data.rpx);
                    that.dealWords({
                        ctx: ctx,
                        //画布上下文
                        fontSize: 14,
                        //字体大小
                        FillStyle: "#333",
                        //字体颜色
                        word: name,
                        //需要处理的文字
                        maxWidth: 80 * that.data.rpx,
                        //一行文字最大宽度
                        x: (102 + 180 * k) * that.data.rpx,
                        //文字在x轴要显示的位置
                        y: 210 * that.data.rpx,
                        //文字在y轴要显示的位置
                        maxLine: 2
                    });
                    that.dealWords({
                        ctx: ctx,
                        fontSize: 14,
                        FillStyle: "#e51a00",
                        word: min_price,
                        maxWidth: 80 * that.data.rpx,
                        x: (102 + 180 * k) * that.data.rpx,
                        y: 250 * that.data.rpx,
                        maxLine: 1
                    });
                    if (param < poster_data.length) {
                        that.makeItem_type1(ctx, poster_data, param + 1);
                    }
                } else if (param >= 4 && param < 6) {
                    let k = param - 4;
                    ctx.drawImage(res.path, (22 + 180 * k) * that.data.rpx, 300 * that.data.rpx, 70 * that.data.rpx, 70 * that.data.rpx);
                    that.dealWords({
                        ctx: ctx,
                        //画布上下文
                        fontSize: 14,
                        //字体大小
                        FillStyle: "#333",
                        //字体颜色
                        word: name,
                        //需要处理的文字
                        maxWidth: 80 * that.data.rpx,
                        //一行文字最大宽度
                        x: (102 + 180 * k) * that.data.rpx,
                        //文字在x轴要显示的位置
                        y: 300 * that.data.rpx,
                        //文字在y轴要显示的位置
                        maxLine: 2
                    });
                    that.dealWords({
                        ctx: ctx,
                        fontSize: 14,
                        FillStyle: "#e51a00",
                        word: min_price,
                        maxWidth: 80 * that.data.rpx,
                        x: (102 + 180 * k) * that.data.rpx,
                        y: 340 * that.data.rpx,
                        maxLine: 1
                    });
                    if (param < poster_data.length) {
                        that.makeItem_type1(ctx, poster_data, param + 1);
                    }
                } else if (param >= 6 && param < 8) {
                    let k = param - 6;
                    ctx.drawImage(res.path, (22 + 180 * k) * that.data.rpx, 390 * that.data.rpx, 70 * that.data.rpx, 70 * that.data.rpx);
                    that.dealWords({
                        ctx: ctx,
                        //画布上下文
                        fontSize: 12,
                        //字体大小
                        FillStyle: "#333",
                        //字体颜色
                        word: name,
                        //需要处理的文字
                        maxWidth: 80 * that.data.rpx,
                        //一行文字最大宽度
                        x: (102 + 180 * k) * that.data.rpx,
                        //文字在x轴要显示的位置
                        y: 390 * that.data.rpx,
                        //文字在y轴要显示的位置
                        maxLine: 2
                    });
                    that.dealWords({
                        ctx: ctx,
                        fontSize: 14,
                        FillStyle: "#e51a00",
                        word: min_price,
                        maxWidth: 80 * that.data.rpx,
                        x: (102 + 180 * k) * that.data.rpx,
                        y: 430 * that.data.rpx,
                        maxLine: 1
                    });
                    if (param < poster_data.length) {
                        that.makeItem_type1(ctx, poster_data, param + 1);
                    }
                } else if (param >= 8 && param < 10) {
                    let k = param - 8;
                    ctx.drawImage(res.path, (22 + 180 * k) * that.data.rpx, 470 * that.data.rpx, 70 * that.data.rpx, 70 * that.data.rpx);
                    that.dealWords({
                        ctx: ctx,
                        //画布上下文
                        fontSize: 14,
                        //字体大小
                        FillStyle: "#333",
                        //字体颜色
                        word: name,
                        //需要处理的文字
                        maxWidth: 80 * that.data.rpx,
                        //一行文字最大宽度
                        x: (102 + 180 * k) * that.data.rpx,
                        //文字在x轴要显示的位置
                        y: 480 * that.data.rpx,
                        //文字在y轴要显示的位置
                        maxLine: 2
                    });
                    that.dealWords({
                        ctx: ctx,
                        fontSize: 14,
                        FillStyle: "#e51a00",
                        word: min_price,
                        maxWidth: 80 * that.data.rpx,
                        x: (102 + 180 * k) * that.data.rpx,
                        y: 520 * that.data.rpx,
                        maxLine: 1
                    });
                    if (param < poster_data.length) {
                        that.makeItem_type1(ctx, poster_data, param + 1);
                    }
                }
            },
            fail() {
                wx.showModal({
                    title: "提示",
                    content: "图片获取失败,图片地址为" + icon,
                    showCancel: false
                });
                if (param < poster_data.length) {
                    that.makeItem_type1(ctx, poster_data, param + 1);
                }
            }
        });
    },
    previewImage: function() {
        let that = this;
        wx.previewImage({
            current: "",
            // 当前显示图片的http链接
            urls: that.data.previewImageUrl
        });
    },
    goBack: function() {
        wx.navigateBack({});
    },
    canvasToImage: function() {
        let that = this;
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 375 * that.data.rpx,
            height: 667 * that.data.rpx,
            destWidth: 4 * 375 * that.data.rpx,
            destHeight: 4 * 667 * that.data.rpx,
            canvasId: "firstCanvas",
            fileType: "png",
            success: function(res) {
                let url = [];
                url.push(res.tempFilePath);
                that.setData({
                    previewImageUrl: url
                });
            },
            fail: function(res) {},
            complete: function(res) {}
        }, this);
    },
    //处理文字多出省略号显示
    dealWords: function(options) {
        options.ctx.setFontSize(options.fontSize);
        //设置字体大小
                options.ctx.setFillStyle(options.FillStyle);
        //设置字体颜色
                var allRow = Math.ceil(options.ctx.measureText(options.word).width / options.maxWidth);
        //实际总共能分多少行
                var count = allRow >= options.maxLine ? options.maxLine : allRow;
        //实际能分多少行与设置的最大显示行数比，谁小就用谁做循环次数
                var endPos = 0;
        //当前字符串的截断点
                for (var j = 0; j < count; j++) {
            var nowStr = options.word.slice(endPos);
            //当前剩余的字符串
                        var rowWid = 0;
            //每一行当前宽度    
                        if (options.ctx.measureText(nowStr).width > options.maxWidth) {
                //如果当前的字符串宽度大于最大宽度，然后开始截取
                for (var m = 0; m < nowStr.length; m++) {
                    rowWid += options.ctx.measureText(nowStr[m]).width;
                    //当前字符串总宽度
                                        if (rowWid > options.maxWidth) {
                        if (j === options.maxLine - 1) {
                            //如果是最后一行
                            options.ctx.fillText(nowStr.slice(0, m - 1) + "...", options.x, options.y + (j + 1) * 18);
                            //(j+1)*18这是每一行的高度        
                                                } else {
                            options.ctx.fillText(nowStr.slice(0, m), options.x, options.y + (j + 1) * 18);
                        }
                        endPos += m;
                        //下次截断点
                                                break;
                    }
                }
            } else {
                //如果当前的字符串宽度小于最大宽度就直接输出
                options.ctx.fillText(nowStr.slice(0), options.x, options.y + (j + 1) * 18);
            }
        }
    }
});